package com.powerunion.datacollection.servlet;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.apache.poi.ss.util.CellRangeAddress;    

public class ExcelExportServlet extends HttpServlet {

	public ExcelExportServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); 
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		OutputStream out = response.getOutputStream();

		response.setContentType("application/vnd.ms-excel;charset=GBK");
		response.addHeader("Content-Disposition",
				"attachment; filename=export.xls");

		String titlestr = request.getParameter("title");
		String headerstr = request.getParameter("header");
		String datastr = request.getParameter("data");

		if (titlestr == null)
			return;
		System.out.println(titlestr);

		String[] titles = titlestr.split("@m@");
		String[] headers = headerstr.split("@m@");
		String[] datas = datastr.split("@m@");

		HSSFWorkbook workbook = new HSSFWorkbook();

		for (int p = 0; p < titles.length; p++) {

			String title = titles[p];
			String header = headers[p];
			String data = datas[p];
			
			
			HSSFSheet sheet = workbook.createSheet(title);

			// 处理所有头部
			String[] sh1 = header.split("@i@");
			for (int i = 0; i < sh1.length; i++) {
				String[] sh2 = sh1[i].split("@k@");
				HSSFRow row = sheet.createRow((short) i);
				int colstart = 0;
				for (int j = 0; j < sh2.length; j++) {
					String sh3 = sh2[j];
					String[] shv = sh3.split(",");
					int cols = Integer.parseInt(shv[1]);
					if (cols > 1) {
						CellRangeAddress range = new CellRangeAddress(i, i,
						colstart, colstart + cols - 1);
				
						sheet.addMergedRegion(range);

					}
					HSSFCell cell = row.createCell(colstart,
							HSSFCell.CELL_TYPE_STRING);
					cell.setCellValue(shv[0]);

					colstart += cols;
				}
			}

			int trow = sh1.length;

			// 处理数据
			String[] sd1 = data.split("@i@");
			for (int i = 0; i < sd1.length; i++) {
				String[] sh2 = sd1[i].split("@k@");
				HSSFRow row = sheet.createRow((short) i + trow);
				for (int j = 0; j < sh2.length; j++) {
					String sh3 = sh2[j];
					HSSFCell cell = row
							.createCell(j, HSSFCell.CELL_TYPE_STRING);
					cell.setCellValue(sh3);
				}
			}

		}

		workbook.write(out);
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
